<?php defined('BASEPATH') or exit('No direct script access allowed');

class diy_diy_model extends CI_Model
{
    private $table;
    public $pluginid;
    public $type;

    private $area_model = null;

    static $IS_MOBLIE = FALSE;

    public function __construct()
    {
        parent::__construct();
        $this->table = $this->db->dbprefix('diy');
        static $CHECK_MOBILE = null;
        if($CHECK_MOBILE === null){
            self::$IS_MOBLIE = $IN_MOBILE = check_wap();
            $CHECK_MOBILE = 1;
        }

        $this->load->driver('cache');
    }

    public function __init_CI($use = 0)
    {
        static $CI = null;
        if($CI && !$use){
            return $CI;
        }
        $CI = new CI_Module();
        return $CI;
    }

    public function cache_key()
    {
        return 'diy:'.$GLOBALS['sid'];
    }

    public function cache_delete()
    {
        return $this->cache->file->delete($this->cache_key());
    }

    function _get_row($where=array()){
        $pluginid = $where['pluginid'];

        static $rdata = array();
        if(isset($rdata[$pluginid])){
            return $rdata[$pluginid];
        }

        $cache_key = $this->cache_key();

        if(!$data = $this->cache->file->get($cache_key))
        {
            $query = $this->db->get_where($this->table,array('sid' => $GLOBALS['sid']));
            foreach ($query->result_array() as $row) {
                $data[$row['pluginid']] = $row;
            }
            $this->cache->file->save($cache_key, $data, 2592000);
        }
        $rdata = $data;
        return $rdata[$pluginid];
    }

    public function insert($data)
    {
        $data['sid'] = $GLOBALS['sid'];
        if(is_array($data)){
            $where = array(
                'pluginid' => $data['pluginid'],
                'sid' => $GLOBALS['sid']
            );
            $this->pluginid = $data['pluginid'];
            $this->type     = $data['type'];
            if($this->_get_row($where))
            {
                $this->db->where($where);
                if($this->db->update($this->table, $data)){
                    $this->cache_delete();
                    return TRUE;
                }
            }
            else
            {
                if($this->db->insert($this->table,$data)){
                    $this->cache_delete();
                    return $this->db->insert_id();
                }
            }
            return false;
        }
        return false;
    }

    public static function line_array($line_number_str)
    {
        $line_number_str = str_replace(array("\t","\r","\n"), '', $line_number_str);
        $line_number_str= str_replace(array('，', ' ', '。', '.', '　'), ',', trim($line_number_str));
        $line_numbers = array_filter(explode(',', $line_number_str));
        return $line_numbers;
    }

    public function __get_line_by_number($line_number_str, $go_list = 0){
        $cat = array(
            1 => '国内',
            2 => '周边',
            3 => '出境',
        );
        $CI = $this->__init_CI(1);
        $CI->load->model('line.Line_model');
        $CI->load->model('line.line_go_model');
        $line_numbers = self::line_array($line_number_str);
        $list = $CI->Line_model->get_line_list($line_numbers, $go_list);
        foreach ($list as $item) {
            $lineids[] = $item['line_id'];
        }

        if(TEMPLATE_NAME =='bai'){
            $end_go_date = $CI->line_go_model->get_line_end_go_date($lineids);
        }

        $tmp_list = array();
        $i = 1 ;
        foreach ($list as $k => $line)
        {
            if(empty($line['line_number'])){
                continue;
            }
            $lm = $line['line_number'];
            $tmp_list[$lm] = $list[$k];
            $tmp_list[$lm]['index_key'] = $i;
            $tmp_list[$lm]['link']             = self::$IS_MOBLIE ? site_url('mobile/line/profile?id='.$line['line_id'].'&eid='.$_COOKIE['from_card'].($this->session->userdata('user_id') ? '&tid='.$this->session->userdata('user_id') : '')) : site_url('xianlu/'.$line['line_id'].'.html');
            $tmp_list[$lm]['line_trait']       = str_replace(array("\t", "\r", "\n"), '', $line['line_trait']);
            $tmp_list[$lm]['line_thumb']       = strpos($line['line_thumb'], 'http://')!== false ?$line['line_thumb']:rtrim(URLPIC, '/').'/'.ltrim($line['line_thumb'] , '/');
            $tmp_list[$lm]['line_thumb_error'] = site_url('static/public/images/nophoto.jpg');
            $tmp_list[$lm]['dollar']           = $line['low_price'] ? '¥' : '';
            $tmp_list[$lm]['dollarend']        = $line['low_price'] ? '起' : '';
            $tmp_list[$lm]['original_price']   = $line['original_price'];
            $tmp_list[$lm]['low_price'] = $tmp_list[$lm]['price'] = $line['low_price'] ? $line['low_price'] : '电询';
            $tmp_list[$lm]['rebate']           = $line['original_price'] ? (sprintf('%.2f', $line['low_price'] / $line['original_price'])*10) : '-';
            $tmp_list[$lm]['youhui'] = $line['youhui'] ;
            $tmp_list[$lm]['go_list']          = $line['go_list'] ? $line['go_list'] .'<a style="color:#65b71f" target="_blank" href="'.$tmp_list[$lm]['link'] .'">更多</a>' : '';
            $tmp_list[$lm]['go_list_new'] = substr($line['go_list'],0,11);
            $tmp_list[$lm]['go_city'] =  $line['go_city'];
            $tmp_list[$lm]['short_name'] =  $line['short_name'];
            $tmp_list[$lm]['product_sort_name'] =  $line['product_sort_name'];
            $tmp_list[$lm]['product_sortname'] =  str_replace('游','', $line['product_sort_name']);
            $tmp_list[$lm]['day_night'] =  $line['days'].'天'.($line['nights'] ? $line['nights'].'晚' : '');
            $end = isset($end_go_date[$k]['end_go_date']) ? $end_go_date[$k]['end_go_date'] : '';
            $diff = get_countdown(strtotime($end)-86400*intval($line['ahead_enroll']));
            $tmp_list[$lm]['end_go_date'] = $diff['day'].'天'.$diff['hour'].'小时'.$diff['min'].'分'.$diff['sec'].'秒';
            $tmp_list[$lm]['end_go_date_short'] = $diff['day'].'天'.$diff['hour'].'时<br>'.$diff['min'].'分'.$diff['sec'].'秒';
            $tmp_list[$lm]['area_name'] = $cat[$line['cat_id']];
            $tmp_list[$lm]['cat'] = $cat[$line['cat_id']];
            $tmp_list[$lm]['cat_str'] = $cat[$line['cat_id']].'游';
            $i++;
        }
        $listdata = array();
        foreach ($line_numbers as $line_number) {
            if(!empty($tmp_list[$line_number])){
                $listdata[$line_number] = $tmp_list[$line_number] ;
            }
        }
        return $listdata;
    }

    public function __get_zlb_line_by_id($line_id_str){
        $CI = $this->__init_CI();
        $CI->load->model('line.Line_zlb_model');
        $line_ids = self::line_array($line_id_str);
        if(!empty($line_ids)){
            foreach($line_ids as $key=>$val){
                $line_ids[$key] = (int)$val;
            }
        }
        $list = $CI->Line_zlb_model->get_zlb_list($line_ids);
        $tmp_list = array();
		
        foreach ($list as $k => $line)
        {
            if(empty($line['line_id'])){
                continue;
            }
            $lm = $line['id'];
            $tmp_list[$lm] = $list[$k];
            $tmp_list[$lm]['link']             = self::$IS_MOBLIE ? site_url('mobile/line/profile?id='.$line['line_id'].'&eid='.$_COOKIE['from_card'].($this->session->userdata('user_id') ? '&tid='.$this->session->userdata('user_id') : '')) : site_url('zlb/xianlu/'. $line['id'].'.html');
            $tmp_list[$lm]['line_thumb']       = $line['line_thumb'];
            $tmp_list[$lm]['line_thumb_error'] = site_url('static/public/images/nophoto.jpg');
            $tmp_list[$lm]['afee']           = $line['afee'];
            $tmp_list[$lm]['aws_price']           = $line['aws_price'] ;
            $tmp_list[$lm]['city']           = $line['city'];
        }
        $listdata = array();
        foreach ($line_ids as $line_id) {
            if(!empty($tmp_list[$line_id])){
                $listdata[] = $tmp_list[$line_id] ;
            }
        }
        return $listdata;
    }

    public function __get_trip_by_id($trips){
        $CI = $this->__init_CI();
        $CI->load->model('trips.Trips_model');
        $line_ids = self::line_array($trips);
        $list = $CI->Trips_model->get_list($line_ids);
        $tmp_list = array();
        foreach ($list as $k => $line)
        {
            if(empty($line['id'])){
                continue;
            }
            $lm = $line['id'];
            $tmp_list[$lm]['link']             = self::$IS_MOBLIE ? site_url('mobile/trips/profile/?id='.$line['id']) : site_url('gonglve/'. $line['id'].'.html');
            $tmp_list[$lm]['thumb']            = filter_img($line['file_url']);
            $tmp_list[$lm]['thumb_error']      = site_url('static/public/images/nophoto.jpg');
            $tmp_list[$lm]['subject']          = $line['subject'];
            $tmp_list[$lm]['author']           = $line['author'] ;
            $tmp_list[$lm]['description']      = $line['abstract'];
            $tmp_list[$lm]['short_desc']      = sub_str($line['abstract'],0,15);
            $tmp_list[$lm]['keywords']         = sub_str($line['keywords'], 0, 20);
            $tmp_list[$lm]['upts']             = date('Y-m-d H:i',$line['upts']);
        }
        $listdata = array();
        foreach ($line_ids as $line_id) {
            if(!empty($tmp_list[$line_id])){
                $listdata[$line_id] = $tmp_list[$line_id] ;
            }
        }
        return $listdata;
    }

    /**
     * 获取团购商品信息
     * @param $goods_ids_str
     * @return array
     */
    public function __get_goods_by_id($goods_ids_str){
        $CI = $this->__init_CI(1);
        $CI->load->model('promotion.Groupgoods_model');
        $goods_ids = self::line_array($goods_ids_str);
        $list = $CI->Groupgoods_model->getGoodsList($goods_ids);
        $tmp_list = array();
        foreach ($list as $k => $line)
        {
            if(empty($line['id'])){
                continue;
            }
            $lm = $line['id'];
            $tmp_list[$lm] = $list[$k];
        }
        $listdata = array();
        foreach ($goods_ids as $good_id) {
            if(!empty($tmp_list[$good_id])){
                $listdata[$good_id] = $tmp_list[$good_id] ;
            }
        }
        return $listdata;
    }

    public function get_data_diy($pluginid)
    {
        $where = array(
            'pluginid' => $pluginid,
        );
        $data = $this->_get_row($where);
        return $data;
    }

    public function get_diy($pluginid, $type, $template = '')
    {
        $this->pluginid = $pluginid;
        $this->type = $type;

        $where = array(
            'pluginid' => $pluginid,
        );
        $data = $this->_get_row($where);

        $type   = $data['type'] ? $data['type'] : $type;
        $svalue = $data['svalue'];
        $svalue = unserialize($svalue);

        $ret = '';
        $method ="process_$type";

        if(method_exists($this, $method)){
            $ret = $this->$method($svalue, $template);
        }
        return $this->init_script($ret, $pluginid, $type);
    }

    public function init_script($ret, $plugin_id, $type)
    {
        if( $this->session->userdata('admin_user_id')<=0){
            return $ret;
        }
        if(!$this->diy_access()){  //会员没有diy权限并且不是超级管理员，则返回
            return $ret;
        }
        static $init = FALSE;
        if(!$init){
            $URLPRE = URLPRE;
            $ret .= <<<INIT
<script type="text/javascript" src="{$URLPRE}static/public/diy/diy.js" ></script>
<script type="text/javascript">var URLPRE = '$URLPRE';$(function(){ diy_model.init();});</script>
INIT;
            $init = TRUE;
        }
        return <<<HTML
$ret
<script type="text/javascript">
document.write('<div id="$plugin_id" style="display:none"></div>');
var cur = jQuery('#$plugin_id');
var dist = document.getElementById('$plugin_id').parentNode;
cur.remove();
jQuery(dist).attr('id', '$plugin_id');
$(function(){diy_model.edit({pluginid:'$plugin_id',type:'$type'});});
</script>
HTML;
    }

    public function diy_access()
    {
        return ($this->session->userdata('admin_user_id')>0 && $this->session->userdata('is_diy') == 1) || CS;
    }

    public function _parse_template($template, $vars)
    {
        $CI = $this->__init_CI();
        $CI->load->library('parser');
        return $CI->parser->parse_string($template, $vars, true);
    }

    /**
     * @param        $svalue
     * @param string $template
     * <code>
     *              {name} 标题
     * </code>
     *
     * @return mixed
     */
    public function process_title($svalue, $template = '')
    {
        return $template ? $this->_parse_template($template, array(
            'name' => $svalue['title']
        )) : $svalue['title'];
    }

    /**
     * @param        $svalue
     * @param string $template
     *       {prolist}<a href="{link}">{name}</a>{/prolist}<a href="{more_link}" >{more_name}</a>
     *
     * @return string
     */
    public function process_prolist($svalue, $template = '')
    {
        $more  = $svalue['e'];
        $svalue = $svalue['t'];
        $prolist = array();
        if(!empty($svalue['area'])){
            foreach ($svalue['area'] as $k => $v) {
                if($v >0){
                    $prolist[] = $v;
                    continue;
                }
                if($svalue['city'][$k]> 0){
                    $prolist[] = $svalue['city'][$k];
                    continue;
                }
                if($svalue['province'][$k]> 0){
                    $prolist[] = $svalue['province'][$k];
                    continue;
                }
                if($svalue['country'][$k]> 0){
                    $prolist[] = $svalue['country'][$k];
                    continue;
                }
                if($svalue['continent'][$k]> 0){
                    $prolist[] = $svalue['continent'][$k];
                    continue;
                }
            }
        }
        $CI = $this->__init_CI();
        $CI->load->model('admin.area_model');

        $areas = $CI->area_model->get_area_name($prolist, false);

        $args = array();
        foreach ($prolist as $areaid)
        {
            $areaname = $areas[$areaid];
            $pinyin = Pinyin($areaname);
            $args[] = array(
                'id'    => $areaid,
                'name'  => $areaname,
                'pinyin'  => $pinyin,
                'link'  => site_url($pinyin),
                'mobile_link' => site_url('mobile/line/?dcity='.$areaid)
            );
        }

        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['prolist'] = $args;
        $ret = $this->_parse_template($template, $data);

        return $ret;
    }

    /**
     * @param        $svalue
     * @param string $template
     *  {linelist}{line_name} {low_price}{/linelist}
     *
     * @return string
     */
    public function process_linelist($svalue, $template = '')
    {
        $list = $this->__get_line_by_number($svalue['line_numbers']);
        if(!$list){
            return '';
        }
        if($list){
            $i=0;
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $i;
                $list[$k]['key'] = $i+1;
                $i++;
            }
        }
        $data = array(
            'linelist' => $list
        );
        return $this->_parse_template($template, $data);
    }

    /**
     * @param        $svalue
     * @param string $template
     *  {linelist}{line_name} {low_price}{/linelist}
     *
     * @return string
     */
    public function process_daren($svalue, $template = '')
    {
        $members_ary = self::line_array($svalue['members']);
        $CI = $this->__init_CI(1);
        $CI->load->model('expert.Expert_model');
        $list = $CI->Expert_model->daren_list($members_ary);
        if(!$list){
            return '';
        }

        if($list){
            $i=0;
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $i;
                $list[$k]['avatar'] = URLPIC . $val['avatar'];
                $list[$k]['link'] = site_url('daren/'.$val['id'].'.html');
                $list[$k]['realname'] = $val['realname'];
                $list[$k]['profile'] = $val['profile'];
                $i++;
            }
        }
        $first = array_shift($list);
        foreach ($first as $k => $v) {
            unset($first[$k]);
            $first['first_'.$k] = $v;
        }

        $data = array_merge( $first,array(
            'others' => array_values($list),
        ));

        return $this->_parse_template($template, $data);
    }

    /**
     * 带头部与链接的线路列表
     * @param        $svalue
     * @param string $template
     *  {linelist}{line_name} {low_price}{/linelist}
     *
     * @return string
     */
    public function process_linetitlelist($svalue, $template = '')
    {
        $i=1;
        $more  = $svalue['e'];
        $list = $this->__get_line_by_number($svalue['line_numbers']);
        if(!$list){
            return '';
        }

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
                $list[$k]['index_key'] = $i;
                $i++;
            }
        }

        $data = array(
            'linelist' => $list
        );
        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['title'] = $more['title'];
        $data['image_url'] = URLPRE;
        $data['template_name'] = TEMPLATE_NAME;
        return $this->_parse_template($template, $data);
    }

    /**
     * 带头部与链接的团购商品列表
     * @param        $svalue
     * @param string $template
     *  {linelist}{line_name} {low_price}{/linelist}
     *
     * @return string
     */
    public function process_tuangoutitlelist($svalue, $template = '')
    {
        $more  = $svalue['e'];
        $list = $this->__get_goods_by_id($svalue['line_numbers']);

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
                $list[$k]['orginal_price'] = intval($val['orginal_price']);
                $list[$k]['current_price'] = intval($val['current_price']);
                $list[$k]['discount'] = sprintf( "%.1f ",intval($val['current_price'])/intval($val['orginal_price']));
            }
        }
        $data = array(
            'goodslist' => $list
        );
        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['title'] = $more['title'];
        $data['image_url'] = URLPRE;
        $data['template_name'] = TEMPLATE_NAME;
        return $this->_parse_template($template, $data);
    }


    public function process_tuangoutablist($svalue, $template = '')
    {
        $nav = array();
        $lists = array();
        $more = $svalue['e'];
        if($svalue['link']){
            $lines = $this->__get_goods_by_id(implode(',', $svalue['line_numbers']));
            foreach ($svalue['link'] as $k => $link) {
                $title = trim($svalue['title'][$k]);
                if(empty($title)){
                    continue;
                }

                $nav[$k]['split'] = $k ==0 ? '' : '|';
                $nav[$k]['link']  = $link? $link : 'javascript:void(0)';
                $nav[$k]['title'] = $title;
                $nav[$k]['index'] = $k;
                $nav[$k]['navon'] = $k == 0 ? 'navOn' : 'navOff';

                $lists[$k]['index'] = $k;
                $lists[$k]['goods_list'] = array();
                foreach (self::line_array($svalue['line_numbers'][$k]) as $linenumber) {
                    if($lines[$linenumber]){
                        $lists[$k]['goods_list'][] = $lines[$linenumber];
                    }
                }
            }
        }

        $data = array(
            'nav' => $nav,
            'lists' => $lists,
            'more_link' => $more['link'],
            'more_name' => $more['name'],
            'title_logo' => $svalue['title_logo'],
            'title' => $more['title'],
        );
        return $this->_parse_template($template, $data);
    }

     /**
     * 攻略列表
     * @param        $svalue
     * @param string $template
     * @return string
     */
    public function process_triplist($svalue, $template = '')
    {
        $more  = $svalue['e'];
        $list = $this->__get_trip_by_id($svalue['trip_ids']);
        if(!$list){
            return '';
        }

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
            }
        }
        $data = array(
            'triplist' => $list
        );
        $more  = $svalue['e'];
        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['title'] = $more['title'];
        $data['image_url'] = URLPRE;
        $data['template_name'] = TEMPLATE_NAME;
        return $this->_parse_template($template, $data);
    }

    public function process_triptablist($svalue, $template = '')
    {
        $nav = array();
        $lists = array();
        $more = $svalue['e'];
        if($svalue['link']){
            $lines = $this->__get_trip_by_id(implode(',', $svalue['trip_ids']));
            $key_value = time_delimit();
            foreach ($svalue['link'] as $k => $link) {
                $title = trim($svalue['title'][$k]);
                if(empty($title)){
                    continue;
                }
                $nav[$k]['split'] = $k ==0 ? '' : '|';
                $nav[$k]['link']  = $link? $link : 'javascript:void(0)';
                $nav[$k]['title'] = $title;
                $nav[$k]['index'] = $k;
                $nav[$k]['navon'] = $k == 0 ? 'navOn' : 'navOff';

                $lists[$k]['index'] = $k;
                $lists[$k]['key']   = $k+1;
                $lists[$k]['title'] = $title;
                $lists[$k]['tripslist'] = array();
                foreach (self::line_array($svalue['trip_ids'][$k]) as $linenumber) {
                    if($lines[$linenumber]){
                        $lists[$k]['tripslist'][] = $lines[$linenumber];
                    }
                }
                //供美食使用
                $key = $lists[$k]['key'];
                if($key_value!=3){
                if($key_value==$key){
                    $lists[$k]['key_value'] = 3;
                }else{
                    if(($key)<5){
                        $lists[$k]['key_value'] = $key+1;
                    }else{
                        $lists[$k]['key_value'] = $key-4;
                    }
                }
                }else{
                    $lists[$k]['key_value'] = $key;
                }
            }
        }
        $data = array(
            'nav' => $nav,
            'lists' => $lists,
            'more_link' => $more['link'],
            'more_name' => $more['name'],
            'title_logo' => $svalue['title_logo'],
            'image_url' =>URLPRE,
            'template_name' =>TEMPLATE_NAME,
            'title' => $more['title'],
        );
        return $this->_parse_template($template, $data);
    }

    public function process_multitablist($svalue, $template = '')
    {
        $nav = array();
        $lists = array();
        $more = $svalue['e'];
        if($svalue['idtype']){
            foreach ($svalue['idtype'] as $k => $idtype) {

                switch($idtype){
                    case 'line':
                        $lines = $this->__get_line_by_number($svalue['ids'][$k]);
                        foreach ($lines as $kk => $line) {
                            $lines[$kk]['thumb'] = $line['line_thumb'];
                            $lines[$kk]['subject'] = $line['line_name'];
                            $lines[$kk]['upts'] = $line['dollar'] . $line['low_price'].$line['dollarend'];
                            $lines[$kk]['author'] = $line['short_name'];
                            $lines[$kk]['thumb_error'] = $line['line_thumb_error'];
                        }
                        break;
                    case 'trips':
                        $lines = $this->__get_trip_by_id($svalue['ids'][$k]);

                        break;
                    case 'ticket':
                        $lines = $this->__get_scenic_by_id($svalue['ids'][$k]);
                        foreach ($lines as $kk => $line) {
                            $lines[$kk]['subject']     = $line['scenic_name'];
                            $lines[$kk]['upts']        = $line['price']?$line['price']:'电询';
                            $lines[$kk]['dollar']        = $line['price'] ? '￥':'';
                            $lines[$kk]['dollar_end']        = $line['price'] ? '起':'';
                            $lines[$kk]['author']      = $line['address'];
                            $lines[$kk]['thumb']       = $line['thumb'];
                            $lines[$kk]['thumb_error'] = $line['thumb_error'];
                        }
                        break;
                }

                $title = trim($svalue['title'][$k]);
                $link = trim($svalue['link'][$k]);
                if(empty($title)){
                    continue;
                }

                $nav[$k]['split'] = $k ==0 ? '' : '|';
                $nav[$k]['link']  = $link? $link : 'javascript:void(0)';
                $nav[$k]['title'] = $title;
                $nav[$k]['index'] = $k;
                $nav[$k]['navon'] = $k == 0 ? 'navOn' : 'navOff';

                $lists[$k]['index'] = $k;
                $lists[$k]['list'] = array();
                foreach (self::line_array($svalue['ids'][$k]) as $linenumber) {
                    if($lines[$linenumber]){
                        $lists[$k]['list'][] = $lines[$linenumber];
                    }
                }
            }
        }
        $data = array(
            'nav' => $nav,
            'lists' => $lists,
            'more_link' => $more['link'],
            'more_name' => $more['name'],
            'title_logo' => $svalue['title_logo'],
        );
        return $this->_parse_template($template, $data);
    }

    /**
     * 景区列表
     * @param        $svalue
     * @param string $template
     *
     * @return string
     */
    public function process_sceniclist($svalue, $template = '')
    {
        $more  = $svalue['e'];
        $list = $this->__get_scenic_by_id($svalue['scenic_ids']);
        if(!$list){
            return '';
        }

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
            }
        }
        $data = array(
            'sceniclist' => $list
        );
        $more  = $svalue['e'];
        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['title'] = $more['title'];
        $data['image_url'] = URLPRE;
        $data['template_name'] = TEMPLATE_NAME;
        return $this->_parse_template($template, $data);
    }

    /**
     * 根据景点ID获取景点信息
     * @param $scenic_ids
     * @return array
     */
    public function __get_scenic_by_id($scenic_ids){
        $CI = $this->__init_CI();
        $CI->load->model('ticket.ticket_model');
        $where = '';
        $list = array();
        if(!empty($scenic_ids)){
            $line_ids = self::line_array($scenic_ids);
            $where = 'b.id in (\''.implode('\',\'',$line_ids).'\')';
            $list = $CI->ticket_model->get_list($where,true,'',1);
        }
        $tmp_list = array();
        foreach ($list as $k => $line)
        {
            if(empty($line['scenic_id'])){
                continue;
            }
            $lm = $line['scenic_id'];

            $tmp_list[$lm] = $line;
            $tmp_list[$lm]['link']             = self::$IS_MOBLIE ? site_url('mobile/line/profile?id='.$line['scenic_id']) : site_url('jingdian/'. $line['scenic_id'].'.html');
            $url = ltrim(str_replace('uploadfile/','', $line['file_url']) , '/');
            $tmp_list[$lm]['thumb']            = strpos($url, 'http://')!== false ? $url : rtrim(URLPIC, '/').'/'.$url;
            $tmp_list[$lm]['thumb_error']      = site_url('static/public/images/nophoto.jpg');
            $tmp_list[$lm]['scenic_name']      = $line['scenic_name'];
            $p      = $CI->ticket_model->get_min_price_by_scenic_id($line['scenic']);
            $tmp_list[$lm]['price'] = strpos($p, '.00')===false ? $p : intval($p);
            if(!$tmp_list[$lm]['price']){
                $tmp_list[$lm]['price_str'] = '电询';
            }else{
                $tmp_list[$lm]['price_str'] = '￥'.$tmp_list[$lm]['price'];
            }
        }
        return $tmp_list;
    }

    /**
     * @param        $svalue
     * @param string $template
     *  {linelist}{line_name} {low_price}{/linelist}
     *  掌旅宝线路列表
     * @return string
     */
    public function process_zlb_linelist($svalue, $template = '')
    {

        $list = $this->__get_zlb_line_by_id($svalue['line_ids']);
        if(!$list){
            return '';
        }

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
                $list[$k]['site_logo'] = 'http://photo.66diqiu.com/uploads/'.$val['fsid'] .'/logo.png';
                $list[$k]['site_logo_error'] = site_url('static/public/images/nologo.jpg');;
            }
        }

        $data = array(
            'zlb_linelist' => $list
        );
        return $this->_parse_template($template, $data);
    }



    public function __get_rent_car_by_id($car_id_str){
        $CI = $this->__init_CI();
        $CI->load->model('rent.rent_model');
        $car_ids = self::line_array($car_id_str);
        if(!empty($car_ids)){
            foreach($car_ids as $key=>$val){
                $car_ids[$key] = (int)$val;
            }
        }
        $list = $CI->rent_model->get_rent_car_list($car_ids);
        $tmp_list = array();
        foreach ($list as $k => $car)
        {
            $lm = $car['id'];
            $tmp_list[$lm] = $list[$k];
            $tmp_list[$lm]['link']             = site_url('zuche');
            $tmp_list[$lm]['show_thumb_error'] = site_url('static/public/images/nophoto.jpg');
         }
        return $tmp_list;
    }

    /**
     * @param        $svalue
     * @param string $template
     *  {rent_carlist}{/rent_carlist}
     *  租车列表
     * @return string
     */
    public function process_rent_carlist($svalue, $template = '')
    {
        $more  = $svalue['e'];
        $list = $this->__get_rent_car_by_id($svalue['car_ids']);
        if(!$list){
            return '';
        }

        if($list){
            foreach ($list as $k => $val) {
                $list[$k]['index'] = $k;
             }
        }


        $data = array(
            'rent_carlist' => $list
        );
        $data['more_link'] = $more['link'];
        $data['more_name'] = $more['name'];
        $data['title'] = $more['title'];
        $data['image_url'] = URLPRE;
        $data['template_name'] = TEMPLATE_NAME;
        return $this->_parse_template($template, $data);
    }

    public function process_linetablist($svalue, $template = '')
    {
        $nav = array();
        $lists = array();
        $more = $svalue['e'];

        $needsort = 0;
        foreach ($svalue['link'] as $ik => $iv) {
            if(!isset($svalue['order'][$ik])){
                $svalue['order'][$ik] = 0;
            }else{
                $needsort = 1;
            }
        }
        if($needsort){
            asort($svalue['order']);
        }
        $step = 0;

        if($svalue['order']){
            $lines = $this->__get_line_by_number(implode(',', $svalue['line_numbers']));
            foreach ($svalue['order'] as $k => $link) {
                $step ++;
                $title = trim($svalue['title'][$k]);
                $link = trim($svalue['link'][$k]);
                if(empty($title)){
                    continue;
                }

                $nav[$k]['split'] = $step == 1 ? '' : '|';
                $nav[$k]['link']  = $link? $link : 'javascript:void(0)';
                $nav[$k]['title'] = $title;
                $nav[$k]['index'] = $k;
                $nav[$k]['navon'] = $step == 1 ? 'navOn' : 'navOff';

                $lists[$k]['index'] = $k;
                $lists[$k]['linelist'] = array();
                foreach (self::line_array($svalue['line_numbers'][$k]) as $linenumber) {
                    if($lines[$linenumber]){
                        $lists[$k]['linelist'][] = $lines[$linenumber];
                    }
                }
            }
        }

        $data = array(
            'nav' => $nav,
            'lists' => $lists,
            'more_link' => $more['link'],
            'more_name' => $more['name'],
            'title_logo' => $svalue['title_logo'],
            'title' => $more['title'],
        );
        return $this->_parse_template($template, $data);
    }

    /**
     * 多样式线路实现
     * @param $svalue
     * @param string $template
     * @return mixed
     */
    public function process_linemoretablist($svalue, $template = '')
    {
        $nav = array();
        $lists = array();
        if($svalue['link']){
            $lines = $this->__get_line_by_number(implode(',', $svalue['line_numbers']));
            $lines_sec = $this->__get_line_by_number(implode(',', $svalue['line_numbers_sec']));
//            print_r($lines_sec);exit;
            foreach ($svalue['link'] as $k => $link) {
                $title = trim($svalue['title'][$k]);
                if(empty($title)){
                    continue;
                }

                $nav[$k]['split'] = $k ==0 ? '' : '|';
                $nav[$k]['link']  = $link? $link : 'javascript:void(0)';
                $nav[$k]['title'] = $title;
                $nav[$k]['index'] = $k;
                $nav[$k]['navon'] = $k == 0 ? 'navOn' : 'navOff';
                $lists[$k]['pic'] = trim($svalue['pic'][$k]);
                $lists[$k]['links'] = $svalue['links'][$k]? $svalue['links'][$k] : 'javascript:void(0)';
                $lists[$k]['index'] = $k;
                $lists[$k]['linelist'] = array();
                foreach (self::line_array($svalue['line_numbers'][$k]) as $linenumber) {
                    if($lines[$linenumber]){
                        $lists[$k]['linelist'][] = $lines[$linenumber];
                    }
                }
                $lists[$k]['linelist_sec'] = array();
                foreach (self::line_array($svalue['line_numbers_sec'][$k]) as $linenumber) {
                    if($lines_sec[$linenumber]){
                        $lists[$k]['linelist_sec'][] = $lines_sec[$linenumber];
                    }
                }
            }
        }
        $data = array(
            'nav' => $nav,
            'lists' => $lists,
            'name' => $svalue['name'],
            'title_logo' => $svalue['title_logo'],
        );
        return $this->_parse_template($template, $data);
    }

    public function process_fontlist($svalue, $template = '')
    {
        $listinfo = array();
        $more = $svalue['e'];
        if($svalue['link']){
            foreach ($svalue['link'] as $k => $link) {
                $title = trim($svalue['title'][$k]);
                if(empty($title)){
                    continue;
                }

                if($link){
                    $link = substr($link, 0, 6) == 'http:/' ? $link : site_url($link);
                }
                $listinfo[$k]['link']  = $link? $link : 'javascript:void(0)';
                $listinfo[$k]['name'] = $title;
                $listinfo[$k]['index'] = $k;
                $listinfo[$k]['key'] = $k+1;
            }
        }

        $data = array(
            'links' => $listinfo,
            'title' => $more['name'],
            'more_name' =>$more['more_name'],
            'more_link' =>$more['more_link'],
        );

        return $this->_parse_template($template, $data);
    }

    /**
     * 机票
     * @param $svalue
     * @param string $template
     * @return mixed
     */
    public function process_flightlist($svalue, $template = '')
    {
        $listinfo = array();
        $more = $svalue['e'];
        if($svalue['scity']){
            foreach ($svalue['scity'] as $k => $scity) {
                if(empty($scity)){
                    continue;
                }
                if($scity){
                    $link = site_url('flight/index/flight_list').'?scity='.$scity.'&ecity='.$svalue['ecity'][$k].'&sdate='.$svalue['sdate'][$k];
                }
                $listinfo[$k]['link']  = $link? $link : 'javascript:void(0)';
                $listinfo[$k]['scity'] = $scity;
                $listinfo[$k]['ecity'] = $svalue['ecity'][$k];
                $listinfo[$k]['price'] = $svalue['price'][$k];
                $listinfo[$k]['sdate'] = $svalue['sdate'][$k];
                $listinfo[$k]['short_date'] = substr($svalue['sdate'][$k],5);
            }
        }

        $data = array(
            'flights' => $listinfo,
            'title' => $more['name'],
        );
        return $this->_parse_template($template, $data);
    }

    /**
     * 酒店
     * @param $svalue
     * @param string $template
     * @return mixed
     */
    public function process_hotellist($svalue, $template = '')
    {
        $listinfo = array();
        $more = $svalue['e'];
        if($svalue['links']){
            foreach ($svalue['links'] as $k => $link) {
                $names = trim($svalue['names'][$k]);
                if(empty($names)){
                    continue;
                }

                if($link){
                    $link = substr($link, 0, 6) == 'http:/' ? $link : site_url($link);
                }
                $listinfo[$k]['links']  = $link? $link : 'javascript:void(0)';
                $listinfo[$k]['names'] = $svalue['names'][$k];
                $listinfo[$k]['areas'] = $svalue['areas'][$k];
                $listinfo[$k]['price'] = $svalue['price'][$k];
                $listinfo[$k]['pic'] = $svalue['pic'][$k];
            }
        }

        $data = array(
            'hotels' => $listinfo,
            'title' => $more['title'],
        );
        return $this->_parse_template($template, $data);
    }


    public function process_imagelist($svalue, $template = '')
    {
        $listinfo = array();
        $more = $svalue['e'];
        if($svalue['titles']){
            foreach ((array)$svalue['titles'] as $k => $vvvv) {
                $pic = trim($svalue['pic'][$k]);
                $title = trim($svalue['titles'][$k]);
                $link = trim($svalue['links'][$k]);
                $sub_title = trim($svalue['sub_titles'][$k]);

                if($link){
                    $link = substr($link, 0, 6) == 'http:/' ? $link : site_url($link);
                }
                $listinfo[$k]['link']  = $link? $link : 'javascript:void(0)';
                $listinfo[$k]['title'] = $title;
                $listinfo[$k]['sub_title'] = $sub_title;
                $listinfo[$k]['price'] = trim($svalue['price'][$k]);
                $listinfo[$k]['pic'] = $pic;
                $listinfo[$k]['index'] = $k;
            }
        }
        if(empty($svalue['orders']) || count($svalue['orders']) != count($listinfo)){
            if(count($listinfo)){
                $svalue['orders'] = range(0, count($listinfo));
            }
        }
        $list = array();
        if($svalue['orders']){
            asort($svalue['orders']);
            $c = count($svalue['orders']);
            foreach ($svalue['orders'] as $kk => $vv) {
                $listinfo[$kk]['aindex'] = $c-$kk;
                $listinfo[$kk]['bindex'] = $kk+1;
                $list[] = $listinfo[$kk];
            }
        }

        $data = array(
            'listinfo' => $list,
            'title' => $svalue['title'],
            'more_name' =>$more['more_name'],
            'more_link' =>$more['more_link'],
        );

        return $this->_parse_template($template, $data);
    }

    public function __destruct(){
    }
}